//
//  GRPayWayView.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/30.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRPayWayView.h"
@interface GRPayWayView()
@property (weak, nonatomic) IBOutlet UIButton *aliPayBt;

@property (weak, nonatomic) IBOutlet UIButton *weXinBt;
@property (nonatomic,weak)UIButton * selectedBt;
@property (weak, nonatomic) IBOutlet UIView *hiddView;


@property (weak, nonatomic) IBOutlet UIButton *payPrice;
@property(nonatomic,weak)id <GRPayWayViewDelegate> delegate;

@end
@implementation GRPayWayView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)awakeFromNib{
    [super awakeFromNib];
    UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenPayView)];
    [self.hiddView addGestureRecognizer:tapGes];
}

-(void)hiddenPayView{
    [self showPayView:NO];
}

+(void)show:(CGFloat)payPrice Delegate:(id <GRPayWayViewDelegate >) delegate{
    GRPayWayView * payWay = [GRPayWayView xx_instantiateFromNib];
    payWay.delegate = delegate;
    [payWay.payPrice setTitle:[NSString stringWithFormat:@"￥%.2lf 确认支付",payPrice] forState:UIControlStateNormal];
    [payWay showPayView:YES];
}

- (IBAction)weXinOrAliPayAction:(UIButton *)sender {
    self.aliPayBt.selected = NO;
    self.weXinBt.selected = NO;
    sender.selected = YES;
    _selectedBt = sender;
}

- (IBAction)payAction:(id)sender {
    [self showPayView:NO];
    if (self.selectedBt) {
        if (_delegate && [_delegate respondsToSelector:@selector(payOrdertWithPayType:)]) {
            [_delegate payOrdertWithPayType:self.selectedBt.tag -100 ];
        }
    }
}

- (IBAction)closePayViewAction:(id)sender {
    [self showPayView:NO];
}

-(void)showPayView:(BOOL)show{
    if (show) {
        UIWindow * window = [UIApplication sharedApplication].keyWindow;
        [window addSubview:self];
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(window);
        }];
    }else{
        [self removeFromSuperview];
    }
}

@end
